package com.wolfden.drugserver.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.wolfden.drugserver.Constants;

public class BankCommandManager implements CommandExecutor {

	public BankCommandManager() {
	}

	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label,
			String[] args) {
		
		Player player = null;

		if (sender instanceof Player) {
			player = (Player) sender;
		}
		
		String command = cmd.getName();

		if (command.equalsIgnoreCase("balance") || command.equalsIgnoreCase("bal")) {
			if (player != null) {
				long balance = Constants.moneyMap.getBalance(player.getName());
				if (balance != -1) {
					sender.sendMessage(player.getName() + " has a balance of "
							+ balance + ".");
				} else {
					sender.sendMessage(player.getName()
							+ " does not have an account.");
				}
			} else {
				sender.sendMessage("This command must be run as a player.");
			}
			return true;
		} else if (command.equalsIgnoreCase("deposit") || command.equalsIgnoreCase("dep")) {
			if (args[0] != null && args[1] != null) {
				if (!(Long.parseLong(args[1]) < 0)) {
					Constants.moneyMap
							.deposit(args[0], Long.parseLong(args[1]));
				} else {
					sender.sendMessage("You may not deposit a negative amount");
				}
			} else {
				sender.sendMessage("You must provide appropriate arguments.");
			}
			return true;
		} else if (command.equalsIgnoreCase("pay")) {
			if (player != null) {
				if (args[0] != null && args[1] != null) {
					if (!player.getName().equalsIgnoreCase(args[0])) {
						if (Long.parseLong(args[1]) >= 0) {
							if (Constants.moneyMap.canAfford(player.getName(),
									Long.parseLong(args[1]))) {
								if (Constants.moneyMap.getBalance(args[0]) != -1) {
									Constants.moneyMap.transfer(
											player.getName(), args[0],
											Long.parseLong(args[1]));
									sender.sendMessage("You have paid "
											+ args[0] + " " + args[1]);
								} else {
									sender.sendMessage("The player you are trying to pay does not have an account.");
								}
							} else {
								sender.sendMessage("You cannot afford to pay this amount.");
							}
						} else {
							sender.sendMessage("You cannot pay a negative amount.");
						}
					} else {
						sender.sendMessage("There is no need to pay yourself.");
					}
				} else {
					sender.sendMessage("You must provide appropriate arguments.");
				}
			} else {
				sender.sendMessage("This command must be run as a player.");
			}
			return true;
		} else if (command.equalsIgnoreCase("transfer") || command.equalsIgnoreCase("tran")) {
			if (args[0] != null || args[1] != null || args[2] != null) {
				if (!(Constants.moneyMap.getBalance(args[0]) == -1 || Constants.moneyMap
						.getBalance(args[1]) == -1)) {
					if (!(Long.parseLong(args[2]) < 0)) {
						if (Constants.moneyMap.canAfford(args[0],
								Long.parseLong(args[2]))) {
							if (!args[0].equalsIgnoreCase(args[1])) {
								Constants.moneyMap.transfer(args[0], args[1],
										Long.parseLong(args[2]));
								sender.sendMessage("You have tranfered "
										+ args[2] + " from " + args[0] + " to "
										+ args[1] + ".");
							} else {
								sender.sendMessage("There is no need to transfer an amount to the same player.");
							}
						} else {
							sender.sendMessage(args[0]
									+ " does not have enough in their account.");
						}
					} else {
						sender.sendMessage("You may not transfer a negative amount.");
					}
				} else {
					sender.sendMessage("One or both of these users does not have an account.");
				}
			} else {
				sender.sendMessage("You must provide appropriate arguments.");
			}
			return true;
		} else if (command.equalsIgnoreCase("userBalance") || command.equalsIgnoreCase("user_bal") || command.equalsIgnoreCase("userbal")) {
			if (args[0] != null) {
				long balance = Constants.moneyMap.getBalance(args[0]);
				if (balance != -1) {
					sender.sendMessage(args[0] + " has a balance of " + balance
							+ ".");
				} else {
					sender.sendMessage(args[0] + " does not have an account.");
				}
			} else {
				sender.sendMessage("You must provide appropriate arguments.");
			}
			return true;
		} else if (command.equalsIgnoreCase("withdraw") || command.equalsIgnoreCase("with")) {
			if (args[0] != null && args[1] != null) {
				if (!(Long.parseLong(args[1]) < 0)) {
					if (Constants.moneyMap.canAfford(args[0],
							Long.parseLong(args[1]))) {
						Constants.moneyMap.withdraw(args[0],
								Long.parseLong(args[1]));
					} else {
						sender.sendMessage(args[0] + " only has "
								+ Constants.moneyMap.getBalance(args[0]));
					}
				} else {
					sender.sendMessage("You may not withdraw a negative amount.");
				}
			} else {
				sender.sendMessage("You must provide appropriate arguments.");
			}
			return true;
		}
		return false;
	}
}